package logic

import (
	"context"
	"fmt"
	"stores/mysql"

	"stores/storesrpc/internal/svc"
	"stores/storesrpc/storesrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type StoreLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewStoreLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StoreLoginLogic {
	return &StoreLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *StoreLoginLogic) StoreLogin(in *storesrpc.StoreLoginRequest) (*storesrpc.StoreLoginResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Debug().Where("account=?", in.Account).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("账号不存在,请前去注册")
	}
	if user.Password != in.Password {
		return nil, fmt.Errorf("密码错误")
	}
	return &storesrpc.StoreLoginResponse{
		Id: int64(user.ID),
	}, nil
}
